package com.badibadi.uniclubber;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.badibadi.activity.AddApplyFormActivity;
import com.badibadi.activity.BaseActivity;
import com.badibadi.adapter.GridViewListAdapter;
import com.badibadi.infos.Discuss;
import com.badibadi.infos.Img;
import com.badibadi.infos.Results;
import com.badibadi.infos.ShowTableModel;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Dialog;
import com.badibadi.mytools.HtmlUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.RegexUtils;
import com.badibadi.mytools.Utils;
import com.libjph.util.JsonUtil;
import com.lidroid.xutils.util.LogUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;

//看申请表
public class SeeApplyFormActivity extends BaseActivity {// item

	private static final String TAG = "SeeApplyFormActivity";

	String uid;
	String cid;
	String apply;
	TextView contents;

	ShowTableModel showTableModel;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				try {
					Utils.ExitPrgress(SeeApplyFormActivity.this);
					Utils.showMessage(SeeApplyFormActivity.this, getResources()
							.getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 2:
				try {
					Utils.ExitPrgress(SeeApplyFormActivity.this);
					initView();
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 3:
				try {
					Utils.ExitPrgress(SeeApplyFormActivity.this);
					Utils.showMessage(SeeApplyFormActivity.this, getResources()
							.getString(R.string.l_xb183));
					finish();
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		goToTheTitle();
		setContentView(R.layout.activity_see_apply_form);

		// intent.putExtra("uid",
		// application_form_Models.get(position).getUser().getId());
		// intent.putExtra("cid",
		// application_form_Models.get(position).getClub().getId());
		// intent.putExtra("apply",
		// application_form_Models.get(position).getId());

		uid = Utils.getUid(SeeApplyFormActivity.this);

		this.findViewById(R.id.activity_mail_back).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
					}
				});
		cid = getIntent().getStringExtra("cid");
		apply = getIntent().getStringExtra("apply");
		// uid="1";
		// cid="11";
		// apply="35";

		init();
	}

	public void initView() {
		GridView item_gridView = (GridView) this.findViewById(R.id.item);
		// 获取所有的包含图片的<src= >类似的html语言
		List<String> list = null;
		if (showTableModel.getContents() != null) {
			list = RegexUtils.getImgSrc(showTableModel.getContents());
//			for (int i = 0; i < list.size(); i++) {
//				LogUtils.d("列表是：==》" + list.get(i).toString());
//			}

		}
		if (list != null && !list.isEmpty()) {
//			LogUtils.d("列表是：==》不是空");
			GridViewListAdapter adapter = new GridViewListAdapter(
					SeeApplyFormActivity.this, list);
			item_gridView.setAdapter(adapter);
			String aa = showTableModel.getContents();
			List<String> listgroup = RegexUtils.getImgSrcGroup(aa);
			for (int i = 0; i < listgroup.size(); i++) {

				// <img src=' ' />
//				LogUtils.d("list.getRoup("+i+")="+list.get(i));
				if (listgroup.get(i).indexOf(Constants.Fei_biaoqing_qianzui) != -1
						|| list.get(i).indexOf(Constants.Fei_biaoqing_qianzui2) != -1
						|| list.get(i).indexOf(Constants.Fei_biaoqing_qianzui3) != -1
						|| list.get(i).indexOf(Constants.Fei_biaoqing_qianzui4) != -1)
					aa = aa.replaceAll(listgroup.get(i), "");// ("<img src='"+list.get(i)+"' />","");

			}

			new HtmlUtils(SeeApplyFormActivity.this, contents, aa)
					.showTextView();
//			LogUtils.d("aa=" + aa);
			// <img src=' '/ >

		} else if (list == null || list.isEmpty()) {
//			LogUtils.d("列表是：==》是空");
			new HtmlUtils(SeeApplyFormActivity.this, contents,
					showTableModel.getContents()).showTextView();
		}
		// new HtmlUtils(SeeApplyFormActivity.this,
		// contents,showTableModel.getContents(),180).showTextView();
	}

	public void init() {
		contents = (TextView) this.findViewById(R.id.contents);
		showTable(uid, cid, apply);
	}

	/**
	 * 查看申请表详细页面-----POST
	 * 
	 * @param $uid
	 *            //用户ID
	 * @param $cid
	 *            //俱乐部ID
	 * @param $apply
	 *            //申请表id
	 */
	public void showTable(final String uid, final String cid, final String apply) {
		Utils.showPrgress(SeeApplyFormActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", uid);
				map.put("cid", cid);
				map.put("apply", apply);
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "web/showTable");
				if (content != null) {
					Results results = Utils.checkResult_NNN(
							SeeApplyFormActivity.this, content);
					if (results != null && !results.getRetmsg().equals("null")
							&& results.isRet() == true) {
						try {
							showTableModel = JsonUtil.getEntityByJsonString(
									results.getRetmsg(), ShowTableModel.class);
							// showDisscuss=discuss.getDiscu().get(0);
							// activityReadingModel=JsonUtil.getEntityByJsonString(results.getRetmsg(),
							// ActivityReadingModel.class);
							handler.sendEmptyMessage(2);
						} catch (Exception e) {
							   
							handler.sendEmptyMessage(3);
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});

	}

}
